package com.azzaou.dao.impl;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.hibernate.Query;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.azzaou.dao.IEmployeDao;
import com.azzaou.model.CategorieEmploye;
import com.azzaou.model.Chantier;
import com.azzaou.model.Employe;
import com.azzaou.model.EmployesAPayer;
import com.azzaou.model.Imputation;
import com.azzaou.model.LigneTransactionChantier;
import com.azzaou.model.PaiementEmploye;

public class EmployeDao extends HibernateDaoSupport implements IEmployeDao,	Serializable {

	private static final long serialVersionUID = 1L;

	@Override
	public List<Employe> getEmployeByCriteria(Employe emp) {

		StringBuffer requete = new StringBuffer("from Employe where 1=1 ");

		if (emp != null) {
			if (!StringUtils.isBlank(emp.getNom())) {
			
				requete.append(" AND nom= :nom ");
			}
			if (!StringUtils.isBlank(emp.getPrenom())) {
			
				requete.append(" AND prenom= :prenom ");
			}
			if (emp.getSalaireJour() != 0) {
			
				requete.append(" AND salaireJour= :salaireJour ");
			
		    if (emp.getSalaireHeure() != 0) {
					
						requete.append(" AND salaireHeure= :salaireHeure ");
		    }
			if(emp.getCategorie()!=null)
			{
				requete.append("And categorie =:categorie.Id");
			}
			}
			
		}

		Query query = getSession().createQuery(requete.toString());

		if (emp != null) {
			if (!StringUtils.isBlank(emp.getNom())) {
				query.setString("nom", emp.getNom());
			}
			if (!StringUtils.isBlank(emp.getPrenom())) {
				query.setString("prenom", emp.getPrenom());
			}
			if (emp.getSalaireJour() != 0) {
				query.setDouble("salaire Journalier", emp.getSalaireJour());
			}

		}

		List<Employe> list = query.list();
		getSession().close();

		return list;
	}
	
	
	
	@Override
	public List<CategorieEmploye> getCategEmployeByCriteria(CategorieEmploye categ) {

		StringBuffer requete = new StringBuffer("from CategorieEmploye where 1=1 ");

		if (categ != null) {
			if (!StringUtils.isBlank(categ.getLibelle())) {
			
				requete.append(" AND libelle= :libelle ");
			}
			if (!StringUtils.isBlank(categ.getDescription())) {
			
				requete.append(" AND description= :description ");
			}
			
		}
        Query query = getSession().createQuery(requete.toString());

		if (categ != null) {
			if (!StringUtils.isBlank(categ.getLibelle())) {
				query.setString("libelle", categ.getLibelle());
			}
			if (!StringUtils.isBlank(categ.getDescription())) {
				query.setString("Description", categ.getDescription());
			}
		}

		List<CategorieEmploye> list = query.list();
		getSession().close();
        return list;
	}

	
	@Override
	public void saveOrUpdateEmploye(Employe employe) {
		
		getHibernateTemplate().saveOrUpdate(employe);
	//	getSession().close();

	}

	
	@Override
	public void removeEmploye(Employe employe) {
		
		getHibernateTemplate().delete(employe);
	//	getSession().close();

	}

	@Override
	public List<String> getAllEmployesByNames() {
		Query query = getSession().createQuery(" select nom from Employe ");
		List<String> list = query.list();
	//	getSession().close();

		return list;
	}


	@Override
	public List<String> getEmployesByNames(String begin) {
		String requet = "select e.nom from Employe e ";
		if(!StringUtils.isBlank(begin))
		{
			requet += "where e.nom like '"+begin+"%'";
		}
		Query query = getSession().createQuery(requet);
		List<String> list = query.list();
		getSession().close();

		return list;
	}


	@Override
	public List<Employe> getAllActifEmploye() {
		List<Employe> employeList = new ArrayList<Employe>();
	    Query query = getSession().createQuery(" FROM Employe where afficher = '1' and dateFinTravail <'"+new Date()+"'");
	    employeList = query.list();
	    getSession().close();

	    return employeList;
		
	}


	@Override
	public List<Employe> getAllEmployeByEchantier(Chantier chantierEnCours) {
		List<Employe> employeList = new ArrayList<Employe>();
	    Query query = getSession().createQuery(" FROM Employe where afficher = '1' and chantier.id ='"+chantierEnCours.getId()+"'");
	    employeList = query.list();
	    getSession().close();

	    return employeList;
	}

	
	@Override
	public List<CategorieEmploye> getAllCategorie() {
		List<CategorieEmploye> categList = new ArrayList<CategorieEmploye>();
	    Query query = getSession().createQuery(" FROM CategorieEmploye ");
	    categList = query.list();
	    getSession().close();

	    return categList;
	}

	@Override
	public List<CategorieEmploye> getAllActifCategorie() {
		List<CategorieEmploye> categList = new ArrayList<CategorieEmploye>();
	    Query query = getSession().createQuery(" FROM CategorieEmploye  where statut = '1' ");
	    categList = query.list();
	    getSession().close();

	    return categList;
	}


	@Override
	public void saveOrUpdateCategorie(CategorieEmploye categorieEmploye) {
		getHibernateTemplate().saveOrUpdate(categorieEmploye);
		getSession().close();

	}


	@Override
	public void removeCategorie(CategorieEmploye categorieEmploye) {
		getHibernateTemplate().delete(categorieEmploye);
		getSession().close();

	}


	@Override
	public void saveImputations(List<Imputation> imputations) {
		for (Imputation imputation : imputations)
		{
			getHibernateTemplate().saveOrUpdate(imputation);
		}
		getSession().close();

	}


	@Override
	public List<Imputation> getImputations(Employe emp) {
		List<Imputation> listImputations = new ArrayList<Imputation>();
	    Query query = getSession().createQuery(" FROM Imputation where employe.id = '"+emp.getId()+"' and dateImputation >'" +emp.getDateDernierePaie()+"'");
	    if(query.list() == null || query.list().isEmpty())
	    {
	    	System.out.println("null");
	    }
	    else{
	    listImputations = query.list();
	    }
	    getSession().close();

	    return listImputations;
	}
	
	
	public List<Imputation> getImutationsPeriod(Chantier chantier, Date dateDebut, Date dateFin)
	{
		List<Imputation> listImputations = new ArrayList<Imputation>();
	    Query query = getSession().createQuery(" FROM Imputation where  dateImputation >='" +new java.sql.Date(dateDebut.getTime())+"' and dateImputation <='"+new java.sql.Date(dateFin.getTime())+"' and employe.chantier ='"+chantier.getId()+"'");
	    if(query.list() == null || query.list().isEmpty())
	    {
	    	System.out.println("null");
	    }
	    else{
	    listImputations = query.list();
	    }
	    getSession().close();

	    return listImputations;
	}
	@Override
	public List<Imputation> getImputationsEmployers(Chantier chantier) {
		List<Imputation> listImputations = new ArrayList<Imputation>();
		
	//	Query queryy = getSession().createQuery("SELECT new EmployesAPayer(employe,sum(imputation.nombreJours), sum(imputation.nombreHeures), employe.salaireHeure, employe.salaireJour) from Imputation where dateImputation > employe.dateDernierePaie");
	    Query query = getSession().createQuery("select  Imputation where dateImputation > employe.dateDernierePaie and employe.chantier.id = '" +chantier.getId()+ "'");
	    if(query.list() == null || query.list().isEmpty())
	    {
	    	System.out.println("null");
	    }
	    else{
	    	listImputations = query.list();
	    }
	    getSession().close();

	    return listImputations;
	}
	
	
	@Override
	public void saveOrUpdatePaiement(PaiementEmploye paiementEmploye) {
		getHibernateTemplate().saveOrUpdate(paiementEmploye);
		getSession().close();

	}



	@Override
	public List<PaiementEmploye> getHistoriqueEmploye(Employe employe) {
		List<PaiementEmploye> paiementEmploye = new ArrayList<PaiementEmploye>();
	    Query query = getSession().createQuery(" FROM PaiementEmploye  where employe = '" +employe.getId()+"'");
	    paiementEmploye = query.list();
	    getSession().close();

	    return paiementEmploye;
	}
	
	
}